﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;

namespace park.System_Manager
{
    public partial class AuthorityUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPageInfo();
            }
        }

        private void LoadPageInfo()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            string json = string.Empty;

            // json = sl.GetAuthorityItems();

            json = "{\"AuthorityItems\":[{\"id\":\"1\",\"name\":\"权限1\"},{\"id\":\"2\",\"name\":\"权限2\"},{\"id\":\"3\",\"name\":\"权限3\"},{\"id\":\"4\",\"name\":\"权限4\"},{\"id\":\"5\",\"name\":\"权限5\"},{\"id\":\"6\",\"name\":\"权限6\"},{\"id\":\"7\",\"name\":\"权限7\"},{\"id\":\"8\",\"name\":\"权限8\"},{\"id\":\"9\",\"name\":\"权限9\"},{\"id\":\"10\",\"name\":\"权限10\"},{\"id\":\"11\",\"name\":\"权限11\"},{\"id\":\"12\",\"name\":\"权限12\"},{\"id\":\"13\",\"name\":\"权限13\"},{\"id\":\"14\",\"name\":\"权限14\"},{\"id\":\"15\",\"name\":\"权限15\"},{\"id\":\"16\",\"name\":\"权限16\"},{\"id\":\"17\",\"name\":\"权限17\"},{\"id\":\"18\",\"name\":\"权限18\"},{\"id\":\"19\",\"name\":\"权限19\"},{\"id\":\"20\",\"name\":\"权限20\"},{\"id\":\"21\",\"name\":\"权限21\"},{\"id\":\"22\",\"name\":\"权限22\"},{\"id\":\"23\",\"name\":\"权限23\"}]}";

            JObject obj = JObject.Parse(json);

            foreach (var item in obj["AuthorityItems"])
            {
                ListItem li = new ListItem(item["name"].ToString(), item["id"].ToString());
                this.cblAuthority.Items.Add(li);
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            string userid = this.txtUserId.Text.Trim();
            string loginName = this.txtLoginName.Text.Trim();
            string name = this.txtUsername.Text.Trim();

            // string join = sl.GetUserAuthority(userid,name,loginName);

            string json = "{\"UserID\":1,\"Username\":\"用户1\",\"LoginName\":\"user1\",\"AuthorityItems\":\"1,3,4,5,6,56\"}";
            

            JObject obj = JObject.Parse(json);

            this.hdfUserid.Value = string.Format("{0}|{1}|{2}", obj["UserID"].ToString(), obj["Username"].ToString(), obj["LoginName"].ToString());
            this.ltUserName.Text = obj["Username"].ToString() + "的权限";

            string authority = obj["AuthorityItems"].ToString();

            string[] arr = authority.Split(',');

            foreach (var item in arr)
            {
                ListItem li = cblAuthority.Items.FindByValue(item);
                if (li != null)
                {
                    li.Selected = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strInfo = this.hdfUserid.Value;
            if (!string.IsNullOrEmpty(strInfo))
            {
                string[] arr = strInfo.Split('|');

                string authority = string.Empty;
                foreach (ListItem item in this.cblAuthority.Items)
                {
                    if (item.Selected)
                    {
                        authority += item.Value + ",";
                    }
                }

                string json = "{" + string.Format("\"UserID\":{0},\"Username\":\"{1}\",\"LoginName\":\"{2}\",\"AuthorityItems\":\"{3}\"", arr[0], arr[1], arr[2], authority.Trim(',')) + "}";

                cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

                // bool flag = sl.UpdateUserAuthority(json);
                bool flag = true;

                string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('您还没有选择任何用户，请先查询要修改的用户。');</script>");
            }
        }
    }
}